#ifndef __SLOTHTTPD_STAGE_SEND_FILE_ASYNC_HPP__
#define __SLOTHTTPD_STAGE_SEND_FILE_ASYNC_HPP__

#include <slothttpd/ref_counted.hpp>
#include <slothttpd/file.hpp>
#include <slothttpd/aio/aio_operation.hpp>
#include <slothttpd/config/location/file_location.hpp>

#include <boost/intrusive_ptr.hpp>

namespace slothttpd {
namespace stage {

class send_headers;

class send_file_async;
typedef boost::intrusive_ptr<send_file_async> send_file_async_ptr;

class send_file_async :
        public ref_counted
{
public:
    send_file_async(connection_ptr conn, const char *path, config::file_location &conf);
    virtual ~send_file_async() noexcept override;
    static void on_headers_sent(send_file_async_ptr s);
    
private:
    void post_async_read();
    static void handle_async_read(send_file_async_ptr r, std::size_t bytes, const boost::system::error_code &err);
    void post_send(std::size_t bytes);
    static void handle_send(send_file_async_ptr r, const boost::system::error_code &err);
    
    char *buffer_;
    std::size_t buffer_size_;
    off_t offset_;
    aio::async_read aread;
    file_ptr responce_file;
    send_headers *header_sender_;
};

}
}

#endif
